// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  // API 调用都保持和云函数当前所在环境一致
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  console.log(event)
  try {
    return await db.collection('user').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        openid: wxContext.OPENID,
        avatarUrl:event.avatarUrl,
        nickName:event.nickName,
        gender:event.gender,
        language:event.language,
        city: event.city,
        province: event.province,
        country: event.country
        },
    })
  } catch(e) {
    console.error(e)
  }
}